﻿using UnityEngine;
namespace CalmNine.Framework
{
#if UNITY_EDITOR
    public class VibrateKit
    {
        public static VibrateKit Global = new VibrateKit();
        public PlayerPrefsBoolVal IsOpen { get; set; }
        private VibrateKit()
        {
            IsOpen = new PlayerPrefsBoolVal(nameof(VibrateKit) + nameof(IsOpen), true);
            Init();
        }

        void Init() { }
        public void DoVibrate(long milliseconds)
        {
            //Debug.Log("Vibrate:" + milliseconds);
        }

        public void CancelVibrate()
        {
            Debug.Log("Stop Vibrate");
        }
#else
    public class VibrateKit
    {
        public static VibrateKit Global = new VibrateKit();
        public PlayerPrefsBoolVal IsOpen { get; set; }
        private VibrateKit()
        {
            IsOpen = new PlayerPrefsBoolVal(nameof(VibrateKit) + nameof(IsOpen), true);
            Init();
        }
        AndroidJavaClass unityPlayer;
        AndroidJavaObject currentActivity;
        AndroidJavaObject vibrator;
        bool CanVibrator {
            get{
                try
                {
                    return IsOpen.Value && vibrator != null && vibrator.Call<bool>("hasVibrator");
                }
                catch 
                {
                    return false;
                }
            }
        }
        void Init()
        {
            try
            {
                unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
                currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
                vibrator = currentActivity.Call<AndroidJavaObject>("getSystemService", "vibrator");
                // 目的是从Unity获取安卓设备的Vibrate权限，但此句不会执行
                if (Application.isEditor)
                    Handheld.Vibrate();
            }
            catch
            {
                vibrator = null;
            }
        }
        void DoVibrate()
        {
            try
            {
                if (CanVibrator)
                    vibrator.Call("vibrate");
            }
            catch
            {
                vibrator = null;
            }
        }


        public void DoVibrate(long milliseconds)
        {
            try
            {
                if (CanVibrator)
                    vibrator.Call("vibrate", milliseconds);
            }
            catch
            {
                vibrator = null;
            }
        }

        void DoVibrate(long[] pattern, int repeat)
        {
            try
            {
                if (CanVibrator)
                    vibrator.Call("vibrate", pattern, repeat);
            }
            catch
            {
                vibrator = null;
            }
        }

        public void CancelVibrate()
        {
            try
            {
                if (CanVibrator)
                {
                    vibrator.Call("cancel");
                }
            }
            catch
            {
                vibrator = null;
            }
        }
#endif
    }
}
